<template>
  <div class="home-container">
    <!-- 欢迎横幅 -->
    <div class="welcome-banner">
      <h1>🍎 Fruit Management System</h1>
      <p>Welcome, {{ currentUser?.name || currentUser?.username }}!</p>
    </div>

    <!-- 功能模块卡片 -->
    <div class="module-grid">
      <!-- 水果分类菜单 -->
      <el-card class="module-card" shadow="hover" @click="goToModule('category')">
        <div class="card-content">
          <div class="card-icon category-icon">
            <el-icon :size="60"><Folder /></el-icon>
          </div>
          <h2>Fruit Category</h2>
          <p>Manage fruit categories and classifications</p>
          <div class="card-footer">
            <el-button type="primary" :icon="ArrowRight">Enter</el-button>
          </div>
        </div>
      </el-card>

      <!-- 水果菜单 -->
      <el-card class="module-card" shadow="hover" @click="goToModule('fruit')">
        <div class="card-content">
          <div class="card-icon fruit-icon">
            <el-icon :size="60"><Orange /></el-icon>
          </div>
          <h2>Fruit Menu</h2>
          <p>Manage fruit inventory and information</p>
          <div class="card-footer">
            <el-button type="success" :icon="ArrowRight">Enter</el-button>
          </div>
        </div>
      </el-card>

      <!-- 仓库菜单 -->
      <el-card class="module-card" shadow="hover" @click="goToModule('warehouse')">
        <div class="card-content">
          <div class="card-icon warehouse-icon">
            <el-icon :size="60"><Box /></el-icon>
          </div>
          <h2>Warehouse</h2>
          <p>Manage warehouse and storage locations</p>
          <div class="card-footer">
            <el-button type="warning" :icon="ArrowRight">Enter</el-button>
          </div>
        </div>
      </el-card>
    </div>

    <!-- 统计信息 -->
    <div class="stats-section" v-loading="loading">
      <el-row :gutter="20">
        <el-col :xs="24" :sm="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <el-icon :size="40" color="#409EFF"><FolderOpened /></el-icon>
              <div class="stat-info">
                <div class="stat-number">{{ stats.categories }}</div>
                <div class="stat-label">Categories</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <el-icon :size="40" color="#67C23A"><ShoppingBag /></el-icon>
              <div class="stat-info">
                <div class="stat-number">{{ stats.fruits }}</div>
                <div class="stat-label">Fruits</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :xs="24" :sm="8">
          <el-card class="stat-card">
            <div class="stat-content">
              <el-icon :size="40" color="#E6A23C"><OfficeBuilding /></el-icon>
              <div class="stat-info">
                <div class="stat-number">{{ stats.warehouses }}</div>
                <div class="stat-label">Warehouses</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { 
  ArrowRight, 
  Folder, 
  Orange, 
  Box, 
  FolderOpened, 
  ShoppingBag, 
  OfficeBuilding
} from '@element-plus/icons-vue';
import { getUser } from '@/utils/auth';
import { statsAPI } from '@/api';

const router = useRouter();
const currentUser = ref(null);
const loading = ref(false);

// 统计数据
const stats = ref({
  categories: 0,
  fruits: 0,
  warehouses: 0
});

// 加载统计数据
const loadStats = async () => {
  loading.value = true;
  try {
    const response = await statsAPI.getStats();
    if (response.success) {
      stats.value = response.data;
    }
  } catch (error) {
    console.error('Failed to load stats:', error);
    ElMessage.error('Failed to load statistics');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  currentUser.value = getUser();
  loadStats();
});

// 进入模块
const goToModule = (module) => {
  switch(module) {
    case 'category':
      router.push('/category');
      break;
    case 'fruit':
      router.push('/fruit');
      break;
    case 'warehouse':
      router.push('/warehouse');
      break;
    default:
      ElMessage.warning('Module under development');
  }
};
</script>

<style scoped>
.home-container {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
}

/* 欢迎横幅 */
.welcome-banner {
  text-align: center;
  padding: 40px 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-radius: 16px;
  color: white;
  margin-bottom: 40px;
  box-shadow: 0 8px 24px rgba(102, 126, 234, 0.3);
}

.welcome-banner h1 {
  font-size: 42px;
  margin: 0 0 10px 0;
  font-weight: 700;
}

.welcome-banner p {
  font-size: 18px;
  margin: 0;
  opacity: 0.9;
}

/* 模块网格 */
.module-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.module-card {
  cursor: pointer;
  transition: all 0.3s ease;
  border-radius: 12px;
  overflow: hidden;
}

.module-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
}

.card-content {
  text-align: center;
  padding: 30px 20px;
}

.card-icon {
  width: 120px;
  height: 120px;
  margin: 0 auto 20px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.3s ease;
}

.module-card:hover .card-icon {
  transform: scale(1.1) rotate(5deg);
}

.category-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.fruit-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.warehouse-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
}

.card-content h2 {
  font-size: 24px;
  margin: 0 0 10px 0;
  color: #333;
}

.card-content p {
  font-size: 14px;
  color: #666;
  margin: 0 0 25px 0;
  line-height: 1.6;
}

.card-footer {
  margin-top: 20px;
}

.card-footer .el-button {
  width: 140px;
  font-size: 16px;
}

/* 统计信息 */
.stats-section {
  margin-top: 40px;
}

.stat-card {
  border-radius: 12px;
  margin-bottom: 20px;
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px;
}

.stat-content .el-icon {
  margin-right: 20px;
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  color: #333;
  line-height: 1;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #999;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .welcome-banner h1 {
    font-size: 32px;
  }

  .welcome-banner p {
    font-size: 16px;
  }

  .module-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }

  .card-icon {
    width: 100px;
    height: 100px;
  }

  .card-icon .el-icon {
    font-size: 50px !important;
  }

  .card-content h2 {
    font-size: 20px;
  }
}
</style>
